/*
 * Copyright 2011 Casey Stehlik, Brad, Tyler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.softeng.network.chess;

import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

public class LoadingMenu
{
	public JFrame mWindow;
	
	public String mGameTitle = "Network Chess";
	
	public int mWidth = 400;
	
	public int mHeight = 145;
	
	public JProgressBar mProgressBar;
	
	public int mProgress;
	
	public JLabel mLoadingLabel;
	
	public String mAddress;
	
	public JButton mCancelButton;
	
	public LoadingMenu(String address)
	{
		this.mAddress = address;
		
		this.mWindow = new JFrame(this.mGameTitle);
		
		this.mWindow.setSize(new Dimension(this.mWidth ,this.mHeight));

		JPanel panel = new JPanel();
		panel.setLayout(null);
		
		this.mLoadingLabel = new JLabel("Waiting for reply from " + this.mAddress);
		this.mLoadingLabel.setSize(300, 25);
		this.mLoadingLabel.setLocation(50, 10);
		this.mLoadingLabel.setHorizontalAlignment(JLabel.CENTER);
		panel.add(this.mLoadingLabel);
		
		this.mProgressBar = new JProgressBar(0, 100);
		this.mProgressBar.setValue(0);
		this.mProgressBar.setStringPainted(true);
		this.mProgressBar.setSize(300, 25);
		this.mProgressBar.setLocation(50, 45);
		panel.add(this.mProgressBar);
		
		this.mCancelButton = new JButton("Cancel");
		this.mCancelButton.setSize(75, 25);
		this.mCancelButton.setLocation(162, 80);
		panel.add(this.mCancelButton);
		
		this.mWindow.setContentPane(panel);
		
		this.mWindow.setResizable(false);
	}
	
	public void display()
	{
		this.mWindow.setVisible(true);
		this.initConnection();
	}
	
	public void initConnection()
	{
		this.mProgress = 0;
		
		while( this.mProgress < 100)
		{
			this.mProgress += 1;
			
			this.mProgressBar.setValue(this.mProgress);
			
			try{ Thread.sleep(10); } catch(Exception e) {}
		}
	}
}
